define([
  'jquery',
  'jqueryui',
  'underscore',
  'backbone',
  'text!templates/search/historyResultsTemplate.html',
  'text!templates/search/detailsTemplate.html',
  'models/appointment/DoctorReadOnlyModel',
], function($,ui, _, Backbone,  historyResultsTemplate,detailsTemplate,DoctorReadOnlyModel){

  var HistoryResultsView = Backbone.View.extend({
    initialize: function(){
    	//_.bindAll('click .historyprint', 'showhistory');
    },
    el: $(".historyresults"),
 //   events: {'click .historyprint' : 'showhistory'} ,
    render: function(){
    	var data = {
    		  results: this.collection.models,
    	        _: _ 
    	      };
	      var compiledTemplate = _.template( historyResultsTemplate, data );
	      $(".historyresults").html(compiledTemplate);
	      var self=this;
	      $('.historyprint').click(function(event) {
	    	    var element = event.currentTarget;
	          	var appointment = self.collection.get(element.id);
	          	
	           var tests = new DoctorReadOnlyModel({id: appointment.get("id") })
	            var lOptions = {};
	            lOptions.success = function(){
	                var eachrow = {
	                		appointment: appointment,
	              		  	examinations: tests.get('examinations'),
	              	        _: _ 
	              	      };
		            var compiledTemplate = _.template( detailsTemplate, eachrow );
		            $(".modal").html(compiledTemplate);
		          $(".modal").lightbox_me({centered: true, onLoad: function() {} });
	               

	            };

	            tests.fetch(lOptions);
	            
	
	      });
    },
    showhistory: function(event){
    	 var element = event.currentTarget;
   	
   }
   });

  return HistoryResultsView;
});
